package org.zjt.domain;

import org.zjt.util.RpcContext;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;

/**
 * Created by zhangjuntao on 2017/4/12.
 */
public class RegisteNodeData {
    private String host ;
    private Integer port ;
    private String className;
    private AtomicBoolean busying ;
    private AtomicLong sendCallTime;
    private AtomicLong CallTime;

    public AtomicBoolean getBusying() {
        return busying;
    }

    public void setBusying(boolean busying) {
        this.busying = new AtomicBoolean(busying);
    }

    public AtomicLong getSendCallTime() {
        return sendCallTime;
    }

    public void setSendCallTime(long sendCallTime) {
        this.sendCallTime = new AtomicLong(sendCallTime);
    }

    public AtomicLong getCallTime() {
        return CallTime;
    }

    public void setCallTime(long callTime) {
        CallTime = new AtomicLong(callTime);
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public RegisteNodeData() {
    }

    public RegisteNodeData(String host, Integer port, String className) {
        this.host = host;
        this.port = port;
        this.className = className;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        RegisteNodeData that = (RegisteNodeData) o;

        if (host != null ? !host.equals(that.host) : that.host != null) return false;
        if (port != null ? !port.equals(that.port) : that.port != null) return false;
        return className != null ? className.equals(that.className) : that.className == null;
    }

    @Override
    public int hashCode() {
        int result = host != null ? host.hashCode() : 0;
        result = 31 * result + (port != null ? port.hashCode() : 0);
        result = 31 * result + (className != null ? className.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "RegisteNodeData{" +
                "host='" + host + '\'' +
                ", port=" + port +
                ", className='" + className + '\'' +
                '}';
    }


    public void initSendState(){
        this.setBusying(Boolean.TRUE);
        this.setSendCallTime(System.currentTimeMillis());
        RpcContext.currentServiceNode = this;
    }


    public void updateReceiveState(){
        this.setBusying(Boolean.FALSE);
        this.setCallTime( System.currentTimeMillis() - this.getSendCallTime().get() );
    }

}
